<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form
      ref="userInfo"
      label-width="220px"
      :model="userInfo"
      :rules="rules"
    >
      <!--手机 -->

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width: 220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hSubmit">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'

export default {
  data() {
    return {
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      // 表单校验-简单
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', triggler: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', triggler: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取员工基本信息
    this.loadUserDetailById()
  },
  methods: {
    // 获取员工基本信息
    async loadUserDetailById() {
      const { data: res } = await getUserDetail(this.$route.query.id)
      // console.log(res)
      this.userInfo = res
    },
    // 点击更新
    hSubmit() {
      this.$refs.userInfo.validate((valid) => {
        if (valid) {
          this.doEdit()
        }
      })
    },
    // 更新申请
    async doEdit() {
      try {
        await saveUserDetailById(this.userInfo)
        this.loadUserDetailById()
        this.$message.success('修改成功')
      } catch (error) {
        this.$message.error('修改失败')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.user-info {
  padding-top: 20px;
}
</style>
